import ViewBase from "./ViewBase";
import Common from "../common/Common";
import GameManager from "../manager/GameManager";
import UIManager from "../manager/UIManager";
import Define from "../common/Define";
import UserInfo from "../UserInfo";
import WXHelper from "../common/WXHelper";

// Learn TypeScript:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/typescript.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html

const {ccclass, property} = cc._decorator;

@ccclass
export default class ReviveViewCtr extends ViewBase {

    @property(cc.Node)
    btn_vedio:cc.Node = null;

    @property(cc.Node)
    btn_gold:cc.Node = null;

    @property(cc.Label)
    texdGold:cc.Label = null;

    @property(cc.Node)
    btn_abandon:cc.Node = null;
    
    //刷新事件
    refreshView(isFristRefresh:boolean = false){
        let count:number = UserInfo.curLevel*10 + 100
        this.texdGold.string = count.toString();
        this.btn_gold.active = false
        this.btn_vedio.active = false

        if(WXHelper.isLoadVideoSuccessful){
            this.btn_vedio.active = true
        }else{
            this.btn_gold.active = true
        }
    }

    //点击点击事件 //只调用一次
    addEvent(){
        Common.addClickEvent(this.btn_vedio,this.onClick.bind(this));
        Common.addClickEvent(this.btn_gold,this.onClick.bind(this));
        Common.addClickEvent(this.btn_abandon,this.onClick.bind(this));
    }

    initView(){
        this.showActionType = Define.left;
    }

    onClick(tag:string){

        console.log("tag ====================== "+tag)
        if(tag == "btn_vedio"){
            WXHelper.showVideo(function(state){
                if(state == 1){
                    UIManager.getInstance().refreshView(Define.viewBattle);
                    GameManager.instance.reviveGame();
                    UIManager.getInstance().hideView(Define.viewRevive);
                }else if(state == 2){ //视频播放达到上限
                    this.btn_vedio.active = false
                    this.btn_gold.active = true
                }
            }.bind(this));
        }else if(tag == "btn_gold"){
            let count:number = UserInfo.curLevel*10 + 100
            if(UserInfo.curGold >= count)
            {
                UserInfo.saveGoldData(count*-1);
                UIManager.getInstance().refreshView(Define.viewBattle);
                GameManager.instance.reviveGame();
                UIManager.getInstance().hideView(Define.viewRevive);
            }else{
                Common.showPrompt("金币不足",900);
                return 
            }
        }else if(tag == "btn_abandon"){
            GameManager.instance.gameOver(null);
            UIManager.getInstance().hideView(Define.viewRevive);
        }
    }
}
